package com.atguigu.gmall.product.component;

import io.minio.MinioClient;
import io.minio.errors.*;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Import;

import java.io.IOException;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;

/**
 * @author 钟涛
 * @version 1.0
 * @description: TODO
 * @date 2021/12/7 21:24
 */
@Configuration
@Import(MinioProperties.class)
public class MinioAutoConfiguration {
    @Bean
    public MinioClient minioClient(MinioProperties minioProperties) throws InvalidPortException, InvalidEndpointException {
        MinioClient minioClient = new MinioClient(minioProperties.getEndpoint(),
                minioProperties.getAccessKey(),
                minioProperties.getSecretKey());
        //检查桶是否存在
        try {
           if(!minioClient.bucketExists(minioProperties.getBucketName())){
               minioClient.makeBucket(minioProperties.getBucketName());
           }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return minioClient;
    }
}
